//
//  AppDelegate.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/10.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        setupAppStyle()
        
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        self.window?.backgroundColor = UIColor.whiteColor()
        
        //设置 跟控制器
        setRootVc()
        
        self.window?.makeKeyAndVisible()
        
        //接收 切换根控制器通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.switchRootViewController(_:)), name: WBNOTIFICATION, object: nil)
        
        return true
    }
    
    //
    func setRootVc() {
        
        if LZTUserAccountViewModel.sharedTools.isLogin {
            
            //已经登录：切换到 欢迎界面
            self.window?.rootViewController = LZTWelcomeViewController()
        } else {
            
            //没有登录：切换到主界面
            self.window?.rootViewController = LZTMainViewController()
        }
    }
    
    //切换跟控制器
    func switchRootViewController(noti: NSNotification) {
        
        if noti.object == nil {
            
            //欢迎界面发送通知：切换到 主控制器
            self.window?.rootViewController = LZTMainViewController()
        } else {
            //OAuth界面发送通知：切换到欢迎界面
            self.window?.rootViewController = LZTWelcomeViewController()
        }
        
    }
    
    //设置app
    func setupAppStyle() {
        
        //设置tabBar的控件颜色，必须在初始化控件前设置
//        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        UINavigationBar.appearance().tintColor = AppThemeColor
        
    }
    //销毁 通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
        //将缓存的微博数据 清除
        LZTStatusDAL.deleteCache()
        
        
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

